package com.group.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.group.pojo.TbCategory;
import com.group.service.CategoryService;

@Controller
@RequestMapping("/category")
public class CategoryController {
	@Autowired
	private CategoryService categoryService;

	// 跳转视图到类别管理页面
	@RequestMapping("/page.action")
	public String page(Model model) {

		return "info/device/category";
	}

	// 查询所有的设备类别
	@RequestMapping("/getAllEquipment.action")
	@ResponseBody
	public List<TbCategory> getAllEquipment(Model model) {
		List<TbCategory> list = categoryService.query(null);
		return list;
	}

	// 根据类别id查询类别名称
	@RequestMapping("/getCategoryNameById.action")
	@ResponseBody
	public String getCatgoryNameById(String categoryId) {
		return categoryService.getCategoryNameById(categoryId);
	}

	// 查看类别名是否存在
	@RequestMapping("/CheckcategoryName.action")
	@ResponseBody
	public String CheckcategoryName(String categoryName) {
		String success = categoryService.CheckcategoryName(categoryName);
		return success;
	}

	// 添加类别时查看父类别名是否存在
	@RequestMapping("/CheckParentCategoryName.action")
	@ResponseBody
	public String CheckParentCategoryName(String parentCategoryName) {
		String success = categoryService.CheckParentCategoryName(parentCategoryName);
		return success;
	}

	// 根据父类别id查询类别名称
	@RequestMapping("/getParentCategoryNameById.action")
	@ResponseBody
	public String getParentCategoryNameById(String parentCategoryId) {
		String parentCategoryName;
		if ("-1".equals(parentCategoryId)) {
			parentCategoryName = "no";
		} else {
			parentCategoryName = categoryService.getParentCategoryNameById(parentCategoryId);
		}
		return parentCategoryName;
	}

	// 添加新的类别
	@RequestMapping("/saveCategory.action")
	@ResponseBody
	public Map<String, String> saveCategory(HttpServletRequest request, TbCategory category) {
		Map<String, String> map = categoryService.saveCategory(request, category);
		return map;
	}

	// 根据类别id查询类别信息
	@RequestMapping("/getCategoryById.action")
	@ResponseBody
	public List<TbCategory> getCategoryById(String Id) {
		return categoryService.getCategoryById(Id);
	}

	// 修改类别信息
	@RequestMapping("/updateSave.action")
	@ResponseBody
	public Map<String, String> updateSave(TbCategory category) {
		Map<String, String> map = categoryService.updateSave(category);
		return map;
	}

	// 删除类别
	@RequestMapping("/batchDelete.action")
	@ResponseBody
	public Map<String, String> batchDelete(String categoryId) {
		Map<String, String> map = categoryService.batchDelete(categoryId);
		return map;
	}

	// 查询类别名是否存在
	@RequestMapping("/categoryCheckRepect.action")
	@ResponseBody
	public String categoryCheckRepect(String categoryName) {
		String success = categoryService.categoryCheckRepect(categoryName);
		return success;
	}

	// 修改类别查询类别名是否存在
	@RequestMapping("/updateCategoryCheckRepect.action")
	@ResponseBody
	public String updateCategoryCheckRepect(String categoryName, String categoryId) {
		String success = categoryService.updateCategoryCheckRepect(categoryName, categoryId);
		return success;
	}

	// 根据id查询类别名是否存在
	@RequestMapping("/isExistSubcategory.action")
	@ResponseBody
	Map<String, String> isExistSubcategory(String parentCategoryId, String categoryStatus) {
		return categoryService.isExistSubcategory(parentCategoryId, categoryStatus);
	}
}
